package com.fenrirtec.aepp.common.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fenrirtec.aepp.common.condition.MemberCondition;
import com.fenrirtec.aepp.common.dao.TenderEnrollDao;
import com.fenrirtec.aepp.common.dao.impl.TenderEnrollDao_JDBCImpl;
import com.fenrirtec.aepp.common.dto.TenderEnrollListDto;
import com.fenrirtec.aepp.common.dto.TenderQuoteDto;
import com.fenrirtec.aepp.common.model.TenderEnrollList;
import com.fenrirtec.aepp.common.service.TenderEnrollService;
import com.fenrirtec.core.database.DatabaseSessionManager;
import com.fenrirtec.core.exception.DatabaseException;
import com.fenrirtec.core.exception.UnexpectedException;
import com.fenrirtec.core.utils.BeanCopierUtils;

public class TenderEnrollServiceImpl implements TenderEnrollService{
	
	private static final Logger logger = LoggerFactory.getLogger(TenderServiceImpl.class);

    @Override
    public List<TenderEnrollListDto> searchByCnd(MemberCondition condition) {
        if (logger.isInfoEnabled()) {
            logger.info("[TenderEnrollServiceImpl#searchByCnd] start.");
        }

        DatabaseSessionManager sessionManager = DatabaseSessionManager.getInstance();
        sessionManager.open();
        Connection connection = sessionManager.getSession();
        List<TenderEnrollList> tenderEnrollList = null;

        try {
            connection.setAutoCommit(false);
            TenderEnrollDao tenderEnrollDao = new TenderEnrollDao_JDBCImpl();
            tenderEnrollList = tenderEnrollDao.searchByCnd(condition);
            connection.commit();
        } catch (DatabaseException e) {
            // expected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderEnrollServiceImpl#searchByCnd] database error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw e;
        } catch (Exception e) {
            // unexpected
            if (logger.isErrorEnabled()) {
                logger.error("[TenderEnrollServiceImpl#searchByCnd] unexpected error occurred.", e);
            }
            try {
                connection.rollback();
            } catch (SQLException e1) {
            }
            throw new UnexpectedException("unexpected error.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[TenderEnrollServiceImpl#searchByCnd] finish.");
            }
            sessionManager.close();
        }
        
        List<TenderEnrollListDto> tenderEnrollDtoList = null;
        
        if (tenderEnrollList != null && tenderEnrollList.size() > 0) {
            tenderEnrollDtoList = new ArrayList<TenderEnrollListDto>();
            for (TenderEnrollList tenderEnroll : tenderEnrollList) {
                TenderEnrollListDto tenderEnrollDto = new TenderEnrollListDto();
                BeanCopierUtils.copyProperties(tenderEnroll, tenderEnrollDto);
                tenderEnrollDtoList.add(tenderEnrollDto);
            }
        }
        
        return tenderEnrollDtoList;
    }

	@Override
	public void updateTenderEnroll(String memberLoginName) {
		if (logger.isInfoEnabled()) {
			logger.info("[TenderEnrollServiceImpl#updateTenderEnroll] start.");
		}

		DatabaseSessionManager sessionManager = DatabaseSessionManager
				.getInstance();
		sessionManager.open();
		Connection connection = sessionManager.getSession();
		try{
			connection.setAutoCommit(false);
			TenderEnrollDao tenderEnrollDao=new TenderEnrollDao_JDBCImpl();
			tenderEnrollDao.updateTenderEnroll(memberLoginName);
			connection.commit();
		}catch(DatabaseException e){
			//expected
			if (logger.isErrorEnabled()) {
				logger.error(
						"[TenderEnrollServiceImpl#updateTenderEnroll] database error occurred.",
						e);
			}
			try {
				connection.rollback();
			} catch (SQLException e1) {
			}
			throw e;
		}catch(Exception e){
			// unexpected
			if (logger.isErrorEnabled()) {
				logger.error(
						"[TenderEnrollServiceImpl#updateTenderEnroll] unexpected error occurred.",
						e);
			}
			try {
				connection.rollback();
			} catch (SQLException e1) {
			}
			throw new UnexpectedException("unexpected error.", e);
		}finally {
			if (logger.isInfoEnabled()) {
				logger.info("[TenderEnrollServiceImpl#updateTenderEnroll] finish.");
			}
			sessionManager.close();
		}
		
	}

	@Override
	public void insertTenderQuote(TenderQuoteDto tenderQuoteDto) {
		if (logger.isInfoEnabled()) {
			logger.info("[TenderEnrollServiceImpl#insertTenderQuote] start.");
		}

		DatabaseSessionManager sessionManager = DatabaseSessionManager
				.getInstance();
		sessionManager.open();
		Connection connection = sessionManager.getSession();
		try{
			connection.setAutoCommit(false);
			TenderEnrollDao tenderEnrollDao=new TenderEnrollDao_JDBCImpl();
			tenderEnrollDao.insertTenderQuote(tenderQuoteDto);
			connection.commit();
		}catch(DatabaseException e){
			//expected
			if (logger.isErrorEnabled()) {
				logger.error(
						"[TenderEnrollServiceImpl#insertTenderQuote] database error occurred.",
						e);
			}
			try {
				connection.rollback();
			} catch (SQLException e1) {
			}
			throw e;
		}catch(Exception e){
			// unexpected
			if (logger.isErrorEnabled()) {
				logger.error(
						"[TenderEnrollServiceImpl#insertTenderQuote] unexpected error occurred.",
						e);
			}
			try {
				connection.rollback();
			} catch (SQLException e1) {
			}
			throw new UnexpectedException("unexpected error.", e);
		}finally {
			if (logger.isInfoEnabled()) {
				logger.info("[TenderEnrollServiceImpl#insertTenderQuote] finish.");
			}
			sessionManager.close();
		}
		
	}
}
